package com.wenlin.userservice.controller;

import com.wenlin.userservice.lock.RedisService;
import com.wenlin.userservice.lock.RedissonService;
import com.wenlin.userservice.response.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 *  Created with IntelliJ IDEA.
 *  Author:  wenlin
 *  Date:  2018/3/26 14:21
 *  Description:
 **/
@RestController
@RequestMapping("/user/redis")
@Api(description = "redis锁测试")
public class RedisController {
    private static Logger logger = LoggerFactory.getLogger(RedisService.class);
    @Autowired
    private RedisService redisService;

    @Autowired
    private RedissonService redissonService;

    @GetMapping("setnx")
    @ApiOperation(value="redis setnx分布式锁")
    public Message<Boolean> getRedisSetNx(){
        Long id = Thread.currentThread().getId();
        logger.info("线程id："+id);
        return redisService.redisExcuteNx();
    }

    @GetMapping("setex")
    @ApiOperation(value="redis setex分布式锁")
    public Message<Boolean> getRedisSetEx(){
        return redisService.redisExcuteEx();
    }

    @GetMapping("trylock")
    @ApiOperation(value="redisson可重入锁")
    public Message<Boolean> getRedissonLock(){
        Long id = Thread.currentThread().getId();
        logger.info("线程id："+id+"请求获取资源锁");
        return redissonService.setLock();
    }

    @GetMapping("fairLock")
    @ApiOperation(value="redisson公平锁")
    public Message<Boolean> getRedissonFairLockLock(){
        Long id = Thread.currentThread().getId();
        String name = Thread.currentThread().getName();
        logger.info("线程id："+id +"线程名字："+name+"开始请求");
        return redissonService.setfairLock();
    }
    @GetMapping("unLock")
    @ApiOperation(value="redisson解锁")
    public String unLock(){
        Long id = Thread.currentThread().getId();
        String name = Thread.currentThread().getName();
        logger.info("线程id："+id +"线程名字："+name);
        return redissonService.unLock();
    }
    @GetMapping("annation")
    @ApiOperation(value="注解")
    public String annation(){
        Long id = Thread.currentThread().getId();
        String name = Thread.currentThread().getName();
        logger.info("线程id："+id +"线程名字："+name);
        redissonService.order();
        return "success";
    }
}
